<template>
  <div class="overview-right">
    <div class="history-overview">
      <div class="title">
        <div class="title-des">历史总览（电站）</div>
      </div>
      <div class="ul">
        <div class="li">
          <div class="li-num">
            {{ formatNum(data.historyData?.totalPower, 4).value
            }}<span
              v-if="formatNum(data.historyData?.totalPower, 4).state"
              class="li-unit"
              >万</span
            >
          </div>
          <div class="li-des">累计充电度数（度）</div>
        </div>
        <div class="li">
          <div class="li-num">
            {{ formatNum(data.historyData?.actualAmount, 2).value
            }}<span
              v-if="formatNum(data.historyData?.actualAmount, 2).state"
              class="li-unit"
              >万</span
            >
          </div>
          <div class="li-des">累计充电金额（元）</div>
        </div>
        <div class="li">
          <div class="li-num">
            {{ formatNum(data.historyData?.count, 0).value
            }}<span
              v-if="formatNum(data.historyData?.count, 0).state"
              class="li-unit"
              >万</span
            >
          </div>
          <div class="li-des">累计充电次数（次）</div>
        </div>
        <div class="li">
          <div class="li-num">
            {{ formatNum(data.historyData?.chargedTime, 2).value
            }}<span
              v-if="formatNum(data.historyData?.chargedTime, 2).state"
              class="li-unit"
              >万</span
            >
          </div>
          <div class="li-des">累计充电时长（小时）</div>
        </div>
      </div>
    </div>
    <div class="emission">
      <div class="title">
        <div class="title-des">绿色出行</div>
      </div>
      <div class="ul">
        <div class="li">
          <div class="li-title">降低碳排放量（kg）</div>
          <div class="li-content">
            <div class="li-bg"></div>
            <div class="li-num">
              {{ data.emissionData.carbonEmission || 0 }}
            </div>
          </div>
        </div>
        <div class="li li1">
          <div class="li-title">相对于植树（棵）</div>
          <div class="li-content">
            <div class="li-bg"></div>
            <div class="li-num">{{ data.emissionData.treeNum || 0 }}</div>
          </div>
        </div>
      </div>
    </div>
    <div class="realtime-charging">
      <div class="title">
        <div class="title-des">当日实时充电分布（{{ nowTime }}）</div>
      </div>
      <div class="ul">
        <chart-realtime :data="data.realtimeData"></chart-realtime>
      </div>
    </div>
  </div>
</template>
<script lang="ts" setup>
import dayjs from 'dayjs';
import ChartRealtime from './chartRealtime.vue';

interface Props {
  data: any;
  time: string;
}
const prop = withDefaults(defineProps<Props>(), {
  data: () => {},
  time: dayjs().format('YYYY-MM-DD'),
});
const nowTime = ref(dayjs().format('YYYY-MM-DD'));
watch(
  () => prop.time,
  (newVal, oldVal) => {
    nowTime.value = dayjs(newVal).format('YYYY-MM-DD');
  }
);

const formatNum = (val: string | number, length: number = 3) => {
  if (isNaN(Number(val))) {
    return {
      value: '--',
      state: false,
    };
  }
  if (Number(val) > 10000) {
    return {
      value: `${parseFloat((Number(val) / 10000).toFixed(2))}`,
      state: true,
    };
  }

  return {
    value: Number(val).toFixed(length) || '--',
    state: false,
  };
};
</script>
<style lang="scss" scoped>
.overview-right {
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  padding-right: 23px;
  position: relative;
  z-index: 2;
  .title {
    width: 467px;
    height: 30px;
    background-image: url('@/assets/images/overView/title-bg.png');
    background-size: 100% 100%;
    background-repeat: no-repeat;
    margin-bottom: 3px;
    &-des {
      padding-top: 11px;
      padding-left: 32px;
      font-size: 12px;
      color: #dfeef3;
    }
  }
  .history-overview {
    margin-bottom: 10px;
    .ul {
      box-sizing: border-box;
      width: 614px;
      height: 332px;
      border-radius: 5px;
      border: 1px solid rgba(48, 172, 243, 0.9);
      display: flex;
      flex-wrap: wrap;
      justify-content: space-around;
      align-items: center;
      padding: 11px 0;
      .li {
        width: 213px;
        height: 116px;
        background-image: url('@/assets/images/overView/icon3.png');
        background-size: 100% 100%;
        background-repeat: no-repeat;
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
        &-num {
          font-size: 36px;
          font-weight: bold;
          color: #50dbff;
        }
        &-des {
          margin-top: 5px;
          width: 125px;
          height: 23px;
          background: linear-gradient(90deg, #0066e5, #00b6ff);
          border-radius: 4px 4px 4px 4px;
          font-size: 12px;
          color: #ffffff;
          text-align: center;
          line-height: 23px;
        }
        &-unit {
          font-weight: normal;
          font-size: 10px;
        }
      }
    }
  }
  .emission {
    margin-bottom: 10px;
    .ul {
      box-sizing: border-box;
      width: 614px;
      height: 230px;
      border-radius: 5px;
      border: 1px solid rgba(48, 172, 243, 0.9);
      display: flex;
      align-items: center;
      .li {
        width: 50%;
        display: flex;
        flex-direction: column;
        box-sizing: border-box;
        padding-left: 47px;
        &-title {
          font-size: 12px;
          color: #ffffff;
          padding-left: 15px;
          padding-bottom: 36px;
        }
        &-content {
          display: flex;
          align-items: center;
        }
        &-bg {
          width: 91px;
          height: 84px;
          background-image: url('@/assets/images/overView/co2-bg.png');
          background-size: 100% 100%;
          background-repeat: no-repeat;
          position: relative;
          &::before {
            content: '';
            position: absolute;
            top: -3px;
            right: -7px;
            width: 83px;
            height: 80px;
            background-image: url('@/assets/images/overView/co2.png');
            background-size: 100% 100%;
            background-repeat: no-repeat;
          }
        }
        &-num {
          font-size: 27px;
          font-weight: bold;
          color: #2eefdb;
          margin-left: 16px;
          white-space: nowrap;
        }
      }
      .li1 {
        .li-bg {
          position: relative;
          &::before {
            content: '';
            position: absolute;
            top: -3px;
            right: 7px;
            width: 71px;
            height: 74px;
            background-image: url('@/assets/images/overView/tree.png');
            background-size: 100% 100%;
            background-repeat: no-repeat;
          }
        }
      }
    }
  }
  .realtime-charging {
    .ul {
      box-sizing: border-box;
      width: 614px;
      height: 256px;
      border-radius: 5px;
      border: 1px solid rgba(48, 172, 243, 0.9);
    }
  }
}
</style>